<?php
/*
** date : 2011-1-21 ; copyright : 7OUR.COM <http://7our.com> ; author : xjay <xucj21@21cn.com>
*/
class DocumentModel extends Model {
	protected $_auto = array(
		array('userid','getLoginUserId',1,'callback'),
		array('ctime','time',3,'function'),
		array('status','1'),
	);
	public function getLoginUserId(){
		return D('User')->getLoginUserId();
	}
	protected function _before_write(&$data){
		if(!empty($data['content'])&&$data['content']!=''){
			$data['content'] = $this->_parseContent($data['content']);
		}
		$arr = array('video','music','at','tag');
		foreach ($arr as $val){
			if(preg_match("/<a type=\"{$val}\"?.*<\/a>/",$data['content'])){
				$data["inc_{$val}"] = '1';
			}else{
				$data["inc_{$val}"] = '0';
			}
		}
	}
	private function _parseContent($content){
		$content = preg_replace("/(?<!\shref=['\"])((https?|ftp|gopher|news|telnet|mms|rtsp):\/\/[a-z0-9\/\-_+=.~!%@?#%&;:$\\│\|]+)/ie", "\$this->_parseContentLink('\\1')", $content);
		$content = preg_replace('/@([^\\&\'"\/\*,<>\r\t\n\s#%?@:：]+)\s?/ie',"\$this->_parseAt('\\1')",$content);
		$code = "/#([A-Za-z0-9".chr(0xa1)."-".chr(0xff)."]+)#/e";
		$content = iconv('gbk','utf-8',preg_replace($code,"\$this->_parseTag('\\1')",iconv('utf-8','gbk',$content)));
		return $content;
	}
	private function _parseContentLink($url){
		if (!($videoAddr = $this->_parseContentVideo($url)) && preg_match("/\.swf\??.*$/i", $url)) {
			$videoAddr = $url;
		}
		if($videoAddr){
			$url = $this->_parseUrl($url);
			return "<a type=\"video\" href=\"{$url}\" data=\"{$videoAddr}\">{$url}</a>";
		}else if(preg_match("/\.(mp3|wma)\??.*$/i", $url)){
			$surl = $this->_parseUrl($url);
			return "<a type=\"music\" href=\"{$surl}\" data=\"{$url}\">{$surl}</a>";
		}else{
			$surl = $this->_parseUrl($url);
			return "<a type=\"url\" href=\"{$surl}\" title=\"$url\">{$surl}</a>";
		}
		
	}
	private function _parseContentVideo($url){
		$parseLink = parse_url($url);
		if (!preg_match("/(youku.com|youtube.com|5show.com|ku6.com|sohu.com|mofile.com|sina.com.cn|tudou.com)$/i", $parseLink['host'], $hosts)) {
			return $this->_getVideoWebSiteAddr($url, $hosts[1]);
		}
		return false;
	}
	private function _getVideoWebSiteAddr($link, $hash){
        $content = file_get_contents($link);//获取
		switch ($hosts) {
			case 'youku.com':
				preg_match("/id\_(\w+)[=.]/",$link,$flashvar);
	        	preg_match("/\+0800\|(.*?)\|\">/i",$content,$img);
	        	preg_match("/<title>(.*?)<\/title>/i",$content,$title);
			case 'ku6.com':
				preg_match("/\/([\w\-]+)\.html/",$link,$flashvar);
	        	preg_match("/<span class=\"s_pic\">(.*?)<\/span>/i",$content,$img);
	        	preg_match("/<title>(.*?)<\/title>/i",$content,$title);
	        	$title[1] = iconv("GBK","UTF-8",$title[1]);
			case 'youtube.com':
				$videoAddr = 'http://www.youtube.com/v/' . $hash;break;
			case 'sina.com.cn':
				preg_match("/vid=(.*?)\/s\.swf/",$content,$flashvar);
	        	preg_match("/pic\:[ ]*\'(.*?)\'/i",$content,$img);
	        	preg_match("/<title>(.*?)<\/title>/i",$content,$title);
			case 'sohu.com':
				 preg_match("/vid=\"(.*?)\"/",$content,$flashvar);
	        	preg_match("/cover=\"(.*?)\";/i",$content,$img);
	        	preg_match("/<title>(.*?)<\/title>/i",$content,$title);
	        	$title[1] = iconv("GBK","UTF-8",$title[1]);
			case 'tudou.com':
				preg_match("/lid\_code[= ]*\'([\w\-]+)\'/",$content,$flashvar);
	        	preg_match("/pic\:[ ]*\"(.*?)\"/i",$content,$img);
	        	preg_match("/<title>(.*?)<\/title>/i",$content,$title);
	        	$title[1] = iconv("GBK","UTF-8",$title[1]);
			case 'mofile.com':
				preg_match("/\/([\w\-]+)\.shtml/",$link,$flashvar);
	        	preg_match("/thumbpath=\"(.*?)\";/i",$content,$img);
	        	preg_match("/<title>(.*?)<\/title>/i",$content,$title);
			default:
				$videoAddr = false;
		}
        return array('flashvar'=>$flashvar[1],'img'=>$img[1],'title'=>$title[1]);
	}
	//解析@标签
	private function _parseAt($content){
		$user = M('User')->where(array('username'=>$content))->count();
		if($user){
			return "<a type=\"at\" href=\"/n/{$user['username']}\">@{$content}<\a>";
		}else{
			return $content;
		}
	}
	//解析##标签
	private function _parseTag($content){
		$dao = M('Tag');
		$stag = iconv('gbk','utf-8',$content);
		$tag = $dao->where("tagname = '$stag'")->find();
		
		if($tag){
			$id = $tag['id'];
			$dao->setInc('hot',array('id'=>$id));
		}else{
			$data['uid'] = $this->getLoginUserId();
			$data['tagname'] = $stag;
			$data['ctime'] = time();
			$data['hot'] = 1;
			$id = $dao->add($data);
		}
		return "<a type=\"tag\" href=\"/t/{$content}\">{$content}</a>";
	}
	private function _parseUrl($url){
		return shortUrl($url);
	}
	public function test(){
		return 'test';
	}
}